package utl.mofen.objects;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import utl.mofen.utils.*;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class JSonStream {	
	private JsonObject jsonObject;
	
	private byte[] stream;	
	
	public JSonStream(JsonObject json, byte[] bytes)
	{
		this.jsonObject = json;
		this.stream = bytes;		
	}
	
	public boolean hasStream()
	{
		boolean ret = false;
		if(stream!=null&&stream.length > 0)
		{
			ret = true;
		}
		return ret;
	}
	
	public void setErrCode(int errCode)
	{
		JsonElement result = jsonObject.get("__result");
		if (result==null)
		{
			result = new JsonObject();
			jsonObject.add("__result", result);
		}
		
		if (result.getAsJsonObject().has("errCode"))
		{
			result.getAsJsonObject().remove("errCode");
		}
		
		result.getAsJsonObject().addProperty("errCode", errCode);	
	}
	
	public void setResultMsg(String msg)
	{		
		JsonElement result = jsonObject.get("__result");
		if (result==null)
		{
			result = new JsonObject();
			jsonObject.add("__result", result);
		}
		
		if (result.getAsJsonObject().has("msg"))
		{
			result.getAsJsonObject().remove("msg");
		}
		
		result.getAsJsonObject().addProperty("msg", msg);		
	}
	
	public void clear()
	{
	   this.jsonObject = new JsonObject();
	   this.stream = null;	   
	}
	
	public void setResult(Boolean value)
	{
		JsonElement result = jsonObject.get("__result");
		if (result==null)
		{
			result = new JsonObject();
			jsonObject.add("__result", result);
		}
		
		if (result.getAsJsonObject().has("result"))
		{
			result.getAsJsonObject().remove("result");
		}
		
		result.getAsJsonObject().addProperty("result", value);
	}

	
	public byte[] getPackageData()
	{
		ByteArrayOutputStream bas=new ByteArrayOutputStream();
		DataOutputStream ds=new DataOutputStream(bas);
		
		String jsonString = GsonUtil.json2String(this.jsonObject);
		
		try {
			byte[] jsonBytes = jsonString.getBytes("utf-8");
			int jsonLength = jsonBytes.length;
			int streamLength = stream.length;
			ds.writeInt(jsonLength);
			ds.writeInt(streamLength);
			ds.write(jsonBytes);
			ds.write(stream);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bas.toByteArray();		
	}
	
	
	/*****
	 * 4 jsonLength
	 * 8 streamLength
	 * jsonString
	 * stream 
	 */
	public void setPackageData(byte[] bytes)
	{
		byte[] bData = null;
		
		ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
		DataInputStream dis=new DataInputStream(bais);	
		
		try {
			int jsonLength = dis.readInt();
			int streamLength = dis.readInt();
			
			bData = new byte[jsonLength];
			
			dis.read(bData);
			
			String jsonStr = new String(bData, "UTF-8");			
			this.jsonObject = GsonUtil.parseObject(jsonStr);			
			
			bData = new byte[streamLength];
			dis.read(bData);
			this.stream = bData;		
			
			dis.close();
			bais.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public JSonStream()
	{
		this.jsonObject = null;
		this.stream = null;		
	}
	
	public void initialize()
	{
		this.jsonObject = new JsonObject();
		this.stream = null;	
	}
	
	public void setJsonObject(JsonObject json)
	{
		this.jsonObject = json;
	}
	
	public void setJsonString(String jsonStr)
	{
		this.jsonObject = GsonUtil.parseObject(jsonStr);
	}
	
	public void setStream(byte[] bytes)
	{
		this.stream = bytes;
	}
	
	public void setResultStreamType(int type)
	{
		JsonElement result = jsonObject.get("__result");
		if (result==null)
		{
			result = new JsonObject();
			jsonObject.add("__result", result);
		}
		
		if (result.getAsJsonObject().has("streamType"))
		{
			result.getAsJsonObject().remove("streamType");
		}
		
		result.getAsJsonObject().addProperty("streamType", type);
	}
	

	public JsonObject getJSonObject()
	{
		return jsonObject;
	}
	
	public byte[] getStream()
	{
		return this.stream;
	}
}
